package com.bytz.modules.cms.common.excel;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelDataConvertException;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.common.exception.ExcelConvertNotFoundException;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

public class ImportExcelListener<T> extends AnalysisEventListener<T> {


    @Getter
    @Setter
    private List<T> dataLs = new ArrayList<>();

    private final Consumer<List<T>> doAfterAllAnalysed;

    public ImportExcelListener() {
        doAfterAllAnalysed = (data) -> {};
    }

    public ImportExcelListener(Consumer<List<T>> doAfterAllAnalysed) {
        this.doAfterAllAnalysed = doAfterAllAnalysed;
    }


    @Override
    public void invoke(T contractObject, AnalysisContext analysisContext) {
        dataLs.add(contractObject);
    }

    @Override
    @Transactional
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        try {
            doAfterAllAnalysed.accept(dataLs);
        } finally {
            dataLs.clear();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) throws Exception {
        dataLs.clear();

        if (exception.getCause() instanceof ExcelConvertNotFoundException) {

           throw new BytzBootException(exception.getCause().getMessage());
        }

        if (exception instanceof ExcelDataConvertException) {
            ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;

            String rightType = "";

            ExcelContentProperty excelContentProperty = excelDataConvertException.getExcelContentProperty();
            Class<?> currentFieldType = excelContentProperty.getField().getType();

            if (currentFieldType == BigDecimal.class) {

                rightType = "，该单元格数据列类型应为数值型 ";
            } else if (currentFieldType == String.class) {

                rightType = "，该单元格数据列类型应为字符型 ";
            } else if (currentFieldType == Date.class) {

                rightType = "，该单元格数据列类型应为日期型 ";
            } else if (currentFieldType == Number.class) {

                rightType = "，该单元格数据列类型应为数值型 ";
            }else if (currentFieldType == LocalDateTime.class) {

                rightType = "，该单元格数据列类型应为日期时间格式如（2022-01-01 00:00:00） ";
            }

            String format = String.format("第【%d】行，第【%s】列解析异常，数据为:【%s】",
                    excelDataConvertException.getRowIndex(),
                    excelDataConvertException.getColumnIndex(),
                    excelDataConvertException.getCellData());

            if (StringUtils.isNotBlank(rightType)) {

                format = format + rightType;
            }

            throw new BytzBootException(format);
        }

        super.onException(exception, context);
    }
}
